<template>
	<view>
		<!-- <van-toast id="van-toast" /> -->
		<nav-back></nav-back>

		<view class="title">
			<text>请选择您的生日 </text>
		</view>
		<view class="tip">最真实的自己，更快遇到最应该的那个人</view>

		<picker-view @change="changeBirthday" :value="birthdayValue" class="my-picker-view">
			<picker-view-column>
				<view class="item" v-for="(item,index) in yearList" :key="index">{{item}} 年</view>
			</picker-view-column>
			<picker-view-column>
				<view class="item" v-for="(item,index) in monthList" :key="index">{{item}} 月</view>
			</picker-view-column>
			<picker-view-column>
				<view class="item" v-for="(item,index) in dayList" :key="index">{{item}} 日</view>
			</picker-view-column>
		</picker-view>

		<button class="next" type="primary" @click="handleNext">下一步</button>
	</view>
</template>

<script>
	import request from '@/utils/request.js'
	import NavBack from '@/components/NavBack.vue'

	export default {
		components: {
			NavBack
		},
		data() {
			return {
				yearList: [],
				monthList: [],
				dayList: [],
				currentYear: '',
				currentMonth: '',
				currentDay: '',
				birthdayValue: [20, 0, 0],
			}
		},
		onLoad() {
			this.inniDateList()
		},
		methods: {
			inniDateList() {
				const currentTime = new Date();
				const currentYear = currentTime.getFullYear();
				for (let i = currentYear; i > currentYear - 100; i--) {
					this.yearList.push(i)
				}
				for (let i = 12; i > 0; i--) {
					this.monthList.push(i)
				}
				this.getDaysInMonth(Number(this.currentYear), Number(this.currentMonth))
				this.currentYear = this.yearList[20]
				this.currentMonth = this.monthList[0]
				this.currentDay = this.dayList[0]
			},
			changeBirthday(e) {
				const val = e.detail.value
				console.log(val, 'changeBirthday');
				if (this.birthdayValue[0] != val[0]) {
					this.birthdayValue[0] = val[0]
					// this.birthdayValue[1] = 0
					this.currentYear = this.yearList[val[0]]
					this.currentMonth = this.monthList[val[1]]
					this.currentDay = this.dayList[val[2]]
				} else if (this.birthdayValue[1] != val[1]) {
					this.birthdayValue[1] = val[1]
					// this.birthdayValue[2] = 0
					this.currentMonth = this.monthList[val[1]]
					this.getDaysInMonth(Number(this.currentYear), Number(this.currentMonth))
					this.currentDay = this.dayList[val[2]]
				} else if (this.birthdayValue[2] != val[2]) {
					this.currentDay = this.dayList[val[2]]
				}
			},
			getDaysInMonth(year, month) {
				let daysInMonth = new Date(year, month, 0).getDate();
				this.dayList = []
				for (let i = daysInMonth; i > 0; i--) {
					this.dayList.push(i)
				}
			},
			handleNext() {
				let birth = `${this.currentYear}-${this.currentMonth}-${this.currentDay}`
				getApp().globalData.userInfo.birthday = birth
				console.log(birth, 'birth');
				uni.redirectTo({
					url: '/pages/userInfo/homeTown/homeTown'
				})
			},
		}
	}
</script>

<style lang="scss">
	/* pages/birth/birth.uniss */
	@import "@/style/register.scss";
</style>